
#include <assert.h>
#include <memory.h>
#include <string.h>
#include <stdlib.h>
#include "cpair.h"

SPair StlcPairCreate( size_t a_szFirstTypeSize, const char* a_pcFirstTypeName, 
					 size_t a_szSecondTypeSize, const char* a_pcSecondTypeName )
{
	SPair stPair = {0};
	memset( &stPair, 0, sizeof( stPair ) );
	assert( a_szFirstTypeSize > 0 );
	assert( a_szSecondTypeSize > 0 );

	stPair.szFirstTypeSize = a_szFirstTypeSize;
	/*memset( stPair.cFirstTypeName, '\0', TYPE_NAME_SIZE + 1 );
	strncpy( stPair.cFirstTypeName, a_pcFirstTypeName, TYPE_NAME_SIZE );
	StlcUnifyTypes( stPair.szFirstTypeSize, stPair.cFirstTypeName);*/

	stPair.szSecondTypeSize = a_szSecondTypeSize;
	/*memset( stPair.cSecondTypeName, '\0', TYPE_NAME_SIZE + 1 );
	strncpy( stPair.cSecondTypeName, a_pcSecondTypeName, TYPE_NAME_SIZE);
	StlcUnifyTypes( stPair.szSecondTypeSize, stPair.cSecondTypeName);*/

	stPair.first = stPair.second = NULL;	
	//stPair.pfnSecondCompare = NULL;
	return stPair;
}

void StlcPairInit( PSPair a_pstPair, PFCompare a_pfnCompFirst, PFCompare a_pfnCompSecond )
{
	assert( NULL != a_pstPair && NULL == a_pstPair->first && NULL == a_pstPair->second );
	//assert(	NULL == a_pstPair->pfnFirstCompare && NULL == a_pstPair->pfnSecondCompare );

	a_pstPair->first = StlcAlloc( a_pstPair->szFirstTypeSize );
	assert( NULL != a_pstPair->first );
	a_pstPair->second = StlcAlloc( a_pstPair->szSecondTypeSize );
	assert( NULL != a_pstPair->second );
	a_pstPair->pfnFirstCompare = ( NULL == a_pstPair->pfnFirstCompare ) ? a_pfnCompFirst : a_pstPair->pfnFirstCompare;
	//a_pstPair->pfnSecondCompare = ( NULL == a_pfnCompSecond ) ? StlcGetCompareFunction( a_pstPair->cSecondTypeName ) : a_pfnCompSecond;
}

bool StlcPairSameType( const SPair* a_pstPairFirst, const SPair* a_pstPairSecond )
{
	if( NULL == a_pstPairFirst || NULL == a_pstPairSecond )
	{
		return false;
	}
	if( a_pstPairFirst->szFirstTypeSize != a_pstPairSecond->szFirstTypeSize
		|| a_pstPairFirst->szSecondTypeSize != a_pstPairSecond->szSecondTypeSize )
	{
		return false;
	}
	/*if( 0 != strncmp( a_pstPairFirst->cFirstTypeName, a_pstPairSecond->cFirstTypeName, TYPE_NAME_SIZE )
		|| 0 != strncmp( a_pstPairFirst->cSecondTypeName, a_pstPairSecond->cSecondTypeName, TYPE_NAME_SIZE ) )
	{
		return false;
	}*/
	if( a_pstPairFirst->pfnFirstCompare != a_pstPairSecond->pfnFirstCompare )
//		|| a_pstPairFirst->pfnSecondCompare != a_pstPairSecond->pfnSecondCompare )
	{
		return false;
	}
	return true;
}

void  StlcPairSet( PSPair a_pstPair, const void* a_pFirst, const void* a_pSecond )
{
	assert( NULL != a_pstPair && NULL != a_pstPair->first && NULL != a_pstPair->second
		&& NULL != a_pstPair->pfnFirstCompare && NULL != a_pFirst && NULL != a_pSecond );
	memcpy( a_pstPair->first, a_pFirst, a_pstPair->szFirstTypeSize );
	memcpy( a_pstPair->second, a_pSecond, a_pstPair->szSecondTypeSize );
}

void StlcPairDestroy( void* a_pPair )
{
	PSPair pstPair= (PSPair)a_pPair;
	assert( NULL != pstPair && NULL != pstPair->first && NULL != pstPair->second );
	StlcFree( pstPair->first );	
	StlcFree( pstPair->second );
	memset( pstPair, 0, sizeof( SPair ) );
}